package com.ruirenyun.util.common;


import com.ruirenyun.entity.constant.SYS;
import com.ruirenyun.util.exception.CommonException;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Deng Ye
 * @version 1.0
 * @date 2020/11/4
 * @description
 * @modifiedBy
 */
public class SubStringUtil {

    public static String hiddenStrWithStar(String source, int headSize, int tailSize) {

        if (StringUtils.isEmpty(source)) {
            return source;
        }
        int starNum = source.length() - headSize - tailSize;
        if (starNum < 0) {
            throw new CommonException(SYS.VAL10006.getValue());
        }
        String head = source.substring(0, headSize);
        String tail = source.substring(headSize + starNum);
        StringBuilder builder = new StringBuilder();
        builder.append(head);
        while (starNum > 0) {
            builder.append('*');
            starNum--;
        }
        builder.append(tail);
        return builder.toString();
    }
}
